<html>
<head>
  <title>JSON - Test Cases</title>
</head>
<body>

<script src="../lib/json.js"></script>
<script src="../lib/time.js"></script>
<script src="../lib/type.js"></script>
<script type="text/javascript">
var printType = function (object) {
  return ''.concat('(', Type.get(object), ') ', object);
};
</script>
<script type="text/javascript">
if (!(window.JSON && window.JSON.Version)) {

document.writeln("JSON doesn't exist. Build with Ruby/Rake.");

} else {

document.write('<pre style="font-size:11px;">');

document.writeln('================================================================================');
document.writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JSON ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
document.writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~~ JSON Parsing and Writing ~~~~~~~~~~~~~~~~~~~~~~~~~~~');
document.writeln('================================================================================');
document.writeln('');

document.writeln('Version : ', JSON.Version);
document.writeln('');

document.writeln('');
document.writeln('--------------------------------------------------------------------------------');
document.writeln('================================================================= TO | STRINGIFY');
document.writeln('--------------------------------------------------------------------------------');
document.writeln('');

document.writeln('Basic:');
document.writeln('');
document.writeln('to(null)   //=> ', JSON.to(null));
document.writeln('to(true)   //=> ', JSON.to(true));
document.writeln('to(false)  //=> ', JSON.to(false));
document.writeln('to("foo")  //=> ', JSON.to('foo'));
document.writeln('to([0,1])  //=> ', JSON.to([0,1]));
document.writeln('to({f:0})  //=> ', JSON.to({f:0}));
document.writeln('');
document.writeln('');
document.writeln('Alias:');
document.writeln('');
document.writeln('stringify("foo")  //=> ', JSON.stringify('foo'));
document.writeln('stringify([0,1])  //=> ', JSON.stringify([0,1]));
document.writeln('stringify({f:0})  //=> ', JSON.stringify({f:0}));
document.writeln('');
document.writeln('');
document.writeln('Strings:');
document.writeln('');
document.writeln('to("foo\"bar\"")    //=> ', JSON.to("foo\"bar\""));
document.writeln('to("\\b\\f\\n\\r\\t")  //=> ', JSON.to("\b\f\n\r\t"));
document.writeln('to("\\x00\\u001e")  //=> ', JSON.to("\x00\u001e"));
document.writeln('to("\\x1f  \\x04")  //=> ', JSON.to("\x1f  \x04"));
document.writeln('');
document.writeln('');
document.writeln('Exclusions:');
document.writeln('');
document.writeln('to({a: 0, f: function(){}})  //=> ', JSON.to({a: 0, f: function(){}}));
document.writeln('');
document.writeln('to({a: new Date(), b: /pattern/, c: 098123})                 //=> ', JSON.to({a: new Date(), b: /pattern/, c: 098123}));
document.writeln('to({a: new Date(), b: /pattern/, c: 098123}, {relax: true})  //=> ', JSON.to({a: new Date(), b: /pattern/, c: 098123}, {relax: true}));
document.writeln('');
document.writeln('');
document.writeln('ASCII Strings:');
document.writeln('');
document.writeln('to("\\x7e")                   //=> ', JSON.to("\x7e"));
document.writeln('to("\\x7e", {ascii: true})    //=> ', JSON.to("\x7e", {ascii: true}));
document.writeln('to("\\x80")                   //=> ', JSON.to("\x80"));
document.writeln('to("\\x80", {ascii: true})    //=> ', JSON.to("\x80", {ascii: true}));
document.writeln('to("\\u2384")                 //=> ', JSON.to("\u2384"));
document.writeln('to("\\u2384", {ascii: true})  //=> ', JSON.to("\u2384", {ascii: true}));
document.writeln('');
document.writeln('to("\\x00")                   //=> ', JSON.to("\x00"));
document.writeln('to("\\x00", {ascii: true})    //=> ', JSON.to("\x00", {ascii: true}));
document.writeln('to("\\x7f")                   //=> ', JSON.to("\x7f"));
document.writeln('to("\\x7f", {ascii: true})    //=> ', JSON.to("\x7f", {ascii: true}));
document.writeln('');
document.writeln('');
document.writeln('Verify:');
document.writeln('');
document.writeln('to({foo : 0}, {verify: true})               //=> ', JSON.to({foo : 0}, {verify: true}));
document.writeln('to(undefined, {verify: true})               //=> ', (function(){try{ return JSON.to(undefined, {verify: true}); } catch(e){ return e.message; }})());
document.writeln('to(undefined, {relax: true, verify: true})  //=> ', (function(){try{ return JSON.to(undefined, {relax: true, verify: true}); } catch(e){ return e.message; }})());
document.writeln('');
document.writeln('');
document.writeln('Relaxing:');
document.writeln('');
document.writeln('to(new Date())                             //=> ', JSON.to(new Date()));
document.writeln('to(new Date(), {relax: true})              //=> ', JSON.to(new Date(), {relax: true}));
document.writeln('to(new Date(), {relax: {date: true}})      //=> ', JSON.to(new Date(), {relax: {date: true}}));
document.writeln('(cycle)                                    //=> ', JSON.to(JSON.from(JSON.to(new Date(), {relax: true}), {relax: true}), {relax: true}));
document.writeln('');
document.writeln('to([undefined])                            //=> ', JSON.to([undefined]));
document.writeln('to([undefined], {relax: true})             //=> ', JSON.to([undefined], {relax: true}));
document.writeln('to([undefined], {relax: {keyword: true}})  //=> ', JSON.to([undefined], {relax: {keyword: true}}));
document.writeln('(cycle)                                    //=> ', JSON.to(JSON.from(JSON.to([undefined], {relax: true}), {relax: true}), {relax: true}));
document.writeln('');
document.writeln('to({0:0, k:9})                             //=> ', JSON.to({0:0, k:9}));
document.writeln('to({0:0, k:9}, {relax: true})              //=> ', JSON.to({0:0, k:9}, {relax: true}));
document.writeln('to({0:0, k:9}, {relax: {objkey: true}})    //=> ', JSON.to({0:0, k:9}, {relax: {objkey: true}}));
document.writeln('(cycle)                                    //=> ', JSON.to(JSON.from(JSON.to({0:0, k:9}, {relax: true}), {relax: true}), {relax: true}));
document.writeln('');

document.writeln('');
document.writeln('--------------------------------------------------------------------------------');
document.writeln('=================================================================== FROM | PARSE');
document.writeln('--------------------------------------------------------------------------------');
document.writeln('');

var longfail = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";

document.writeln('Basic:');
document.writeln('');
document.writeln('from(" \\"foo\\" ")  //=> ', printType(JSON.from(' \"foo\" ')));
document.writeln('from("-0.24e+12")  //=> ', printType(JSON.from('-0.24e+12')));
document.writeln('from("  true   ")  //=> ', printType(JSON.from('  true   ')));
document.writeln('from("    false")  //=> ', printType(JSON.from('    false')));
document.writeln('from("null     ")  //=> ', printType(JSON.from('null     ')));
document.writeln('from("[ \"foo\" ]")  //=> ', printType(JSON.from('[ "foo" ]')));
document.writeln('from("[20, 1.4]")  //=> ', printType(JSON.from('[20, 1.4]')));
document.writeln('from("{\"f\": 20}")  //=> ', printType(JSON.from('{"f": 20}')));
document.writeln('');
document.writeln('');
document.writeln('Alias:');
document.writeln('');
document.writeln('parse(" \\"foo\\" ")  //=> ', printType(JSON.parse(' \"foo\" ')));
document.writeln('parse("-0.24e+12")  //=> ', printType(JSON.parse('-0.24e+12')));
document.writeln('parse("  true   ")  //=> ', printType(JSON.parse('  true   ')));
document.writeln('');
document.writeln('');
document.writeln('Errors:');
document.writeln('');
document.writeln('from("         ")  //=> ', (function(){try{ return printType(JSON.from('         ')); }catch(e){return e.message;}})());
document.writeln('from("[0]   [1]")  //=> ', (function(){try{ return printType(JSON.from('[0]   [1]')); }catch(e){return e.message;}})());
document.writeln('from(" {\"foo\"} ")  //=> ', (function(){try{ return printType(JSON.from(' {"foo"} ')); }catch(e){return e.message;}})());
document.writeln('');
document.writeln('');
document.writeln('Error Length:');
document.writeln('');
document.writeln('from("a..z" * 4)                 //=> ', (function(){try{ return printType(JSON.from(longfail)); }catch(e){return e.message;}})());
document.writeln('from("a..z" * 4, {errlen:  10})  //=> ', (function(){try{ return printType(JSON.from(longfail, {errlen:  10})); }catch(e){return e.message;}})());
document.writeln('from("a..z" * 4, {errlen:   2})  //=> ', (function(){try{ return printType(JSON.from(longfail, {errlen:   2})); }catch(e){return e.message;}})(), '  // minimum of 10');
document.writeln('from("a..z" * 4, {errlen:  20})  //=> ', (function(){try{ return printType(JSON.from(longfail, {errlen:  20})); }catch(e){return e.message;}})());
document.writeln('from("a..z" * 4, {errlen:  50})  //=> ', (function(){try{ return printType(JSON.from(longfail, {errlen:  50})); }catch(e){return e.message;}})());
document.writeln('from("a..z" * 4, {errlen: 100})  //=> ', (function(){try{ return printType(JSON.from(longfail, {errlen: 100})); }catch(e){return e.message;}})());
document.writeln('from("a..z" * 4, {errlen: 125})  //=> ', (function(){try{ return printType(JSON.from(longfail, {errlen: 125})); }catch(e){return e.message;}})());
document.writeln('');
document.writeln('');
document.writeln('Relaxing:');
document.writeln('');
document.writeln('from("{f: 20}", {relax: true})     //=> ', printType(JSON.from("{f: 20}", {relax: true})));
document.writeln('from("\'quote\'", {relax: true})     //=> ', printType(JSON.from("'quote'", {relax: true})));
document.writeln('from("0x14af9", {relax: true})     //=> ', printType(JSON.from("0x14af9", {relax: true})));
document.writeln('from("0716253", {relax: true})     //=> ', printType(JSON.from("0716253", {relax: true})));
document.writeln('from("undefined", {relax: true})   //=> ', printType(JSON.from("undefined", {relax: true})));
document.writeln('from("{f: 20}", {relax: false})    //=> ', (function(){try{ return printType(JSON.from("{f: 20}", {relax: false})); }catch(e){return e.message;}})());
document.writeln('from("\'quote\'", {relax: false})    //=> ', (function(){try{ return printType(JSON.from("'quote'", {relax: false})); }catch(e){return e.message;}})());
document.writeln('from("0x14af9", {relax: false})    //=> ', (function(){try{ return printType(JSON.from("0x14af9", {relax: false})); }catch(e){return e.message;}})());
document.writeln('from("0716253", {relax: false})    //=> ', (function(){try{ return printType(JSON.from("0716253", {relax: false})); }catch(e){return e.message;}})());
document.writeln('from("undefined", {relax: false})  //=> ', (function(){try{ return printType(JSON.from("undefined", {relax: false})); }catch(e){return e.message;}})());
document.writeln('');
document.writeln('from("\\"2008-04-02T12:24:48Z\\"")                             //=> ', printType(JSON.from('"2008-04-02T12:24:48Z"')));
document.writeln('from("\\"2008-04-02T12:24:48Z\\"", {relax: {date: true}})      //=> ', printType(JSON.from('"2008-04-02T12:24:48Z"', {relax: {date: true}})));
document.writeln('from("\\"2008-04-02T12:24:48.999Z\\"", {relax: {date: true}})  //=> ', printType(JSON.from('"2008-04-02T12:24:48.999Z"', {relax: {date: true}})));
document.writeln('');

document.write('</pre>');

}
</script>

</body>
</html>
